package com.home.generate;

import com.custom.comm.enums.KeyStrategy;
import com.custom.comm.enums.TableNameStrategy;
import com.custom.generator.FreemarkerTemplateExecutor;
import com.custom.generator.config.GenarateGlobalConfig;
import com.custom.generator.config.PackageConfig;
import com.custom.generator.config.TableConfig;
import com.custom.generator.core.GenerateCodeExecutor;
import com.custom.generator.model.TableStructModel;
import com.custom.jdbc.configuration.DbCustomStrategy;
import com.custom.jdbc.configuration.DbDataSource;
import com.custom.jdbc.configuration.DbGlobalConfig;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * @author XiaoBai
 * @since 2024/3/4 14:00
 */
public class ModelBuilder {


    public void build() throws IOException {

        Properties properties = new Properties();
        properties.load(Files.newInputStream(new File(String.format("D:\\MyTest\\jdbcConnection-%s.properties", "fly")).toPath()));
        DbDataSource dataSource = new DbDataSource(properties, "custom-jdbc");
        DbGlobalConfig globalConfig = new DbGlobalConfig();

        DbCustomStrategy strategy = new DbCustomStrategy();
        strategy.setUnderlineToCamel(true);
        strategy.setSqlOutPrinting(true);
        strategy.setDbFieldDeleteLogic("state");
        strategy.setDeleteLogicValue(1);
        strategy.setNotDeleteLogicValue(0);

        globalConfig.setStrategy(strategy);
        GenerateCodeExecutor executor = new GenerateCodeExecutor(dataSource, globalConfig);

        // 生成的表
        executor.setTables(new String[]{
                "fly_category",
                "fly_dictionary",
                "fly_element",
                "fly_order_info",
                "fly_product_element",
                "fly_product_img",
                "fly_product_info",
                "fly_product_make",
                "fly_product_spec",
                "fly_shop",
                "fly_shop_cart",
                "fly_user_info"
        });

        // 表与实体的配置
        TableConfig tableConfig = new TableConfig();
        tableConfig.setTablePrefix("fly");
        tableConfig.setEntitySuffix("PO");
        tableConfig.setEntityDbFieldAnnotationValueEnable(true);
        tableConfig.setParentEntityClass(BaseTableInfo.class);
        executor.setTableConfig(tableConfig);


        // 生成位置以及生成的包名配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParentPackage("com.home.testgenerate");
        packageConfig.setEntity("entity");
        executor.setPackageConfig(packageConfig);

        GenarateGlobalConfig ggconfig = new GenarateGlobalConfig();
        ggconfig.setAuthor("XiaoBai");
        ggconfig.setKeyStrategy(KeyStrategy.AUTO);
        ggconfig.setOverrideEnable(true);
        ggconfig.setEntityLombok(true);
        ggconfig.setSwagger(true);
        executor.setGlobalConfig(ggconfig);
        executor.start();

        FreemarkerTemplateExecutor templateExecutor = new FreemarkerTemplateExecutor();

        List<TableStructModel> tableStructModels = executor.getTableStructModels();
        for (TableStructModel model : tableStructModels) {
            templateExecutor.buildEntity(model);
        }

    }

}
